<template>
  <!-- 新增部门的弹层 -->
  <el-dialog :title="dialogTitle" :visible="isShow" @close="handleClose">
    <!-- 表单组件  el-form   label-width设置label的宽度   -->
    <!-- 匿名插槽 -->
    <el-form
      ref="deptForm"
      :model="deptForm"
      :rules="deptRules"
      label-width="120px"
    >
      <el-form-item label="部门名称" prop="name">
        <el-input
          v-model="deptForm.name"
          style="width: 80%"
          placeholder="1-50个字符"
        />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input
          v-model="deptForm.code"
          style="width: 80%"
          placeholder="1-50个字符"
        />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select
          v-model="deptForm.manager"
          style="width: 80%"
          placeholder="请选择"
        >
          <el-option
            v-for="item in peoples"
            :key="item.id"
            :label="item.username"
            :value="item.username"
          /></el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input
          v-model="deptForm.introduce"
          style="width: 80%"
          placeholder="1-300个字符"
          type="textarea"
          :rows="3"
        />
      </el-form-item>
    </el-form>
    <!-- el-dialog有专门放置底部操作栏的 插槽  具名插槽 -->
    <div slot="footer">
      <el-button type="primary" size="small" @click="submit">确定</el-button>
      <el-button size="small" @click="handleClose">取消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { reqAddDeparts, reqGetDepartsDetails, reqUpdateDepartsDetails } from '@/api/departments'
import { reqGetEmployeeSimple } from '@/api/employees'
export default {
  name: 'AddDept',
  props: {
    isShow: {
      type: Boolean,
      default: false
    },
    deptList: {
      type: Array,
      required: true
    },
    nodeData: {
      type: Object,
      required: true
    }
  },
  data() {
    const validateName = (rule, value, callback) => {
      console.log(value, this.nodeData.id, this.deptList)
      let res = []
      if (this.form.id) {
        if (this.nodeData.name === value) {
          callback()
          return
        }
        res = this.deptList.filter((item) => item.pid === this.nodeData.pid)
      } else {
        res = this.deptList.filter((item) => item.pid === this.nodeData.id)
      }
      const isRepeat = res.some((item) => item.name === value)
      isRepeat ? callback(new Error('同级部门中已存在该部门名称')) : callback()
    }
    const validateCode = (rule, value, callback) => {
      if (this.deptForm.id && value === this.nodeData.code) {
        callback()
        return
      }
      const isRepeat = this.deptList.some((item) => item.code === value)
      isRepeat ? callback(new Error('部门编码已存在')) : callback()
    }
    return {
      deptForm: {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      },
      peoples: [],

      deptRules: {
        name: [
          {
            required: true,
            message: '部门名称不能为空',
            trigger: ['blur', 'change']
          },
          {
            min: 1,
            max: 10,
            message: '部门名称只能是1-10位',
            trigger: ['blur']
          },
          {
            validator: validateName,
            trigger: 'blur'
          }
        ],
        code: [
          {
            required: true,
            message: '部门编码不能为空',
            trigger: ['blur', 'change']
          },
          {
            min: 1,
            max: 10,
            message: '部门编码只能是1-10位',
            trigger: ['blur']
          },
          {
            validator: validateCode,
            trigger: 'blur'
          }
        ],
        manager: [
          {
            required: true,
            message: '部门负责人不能为空',
            trigger: ['blur', 'change']
          }
        ],
        introduce: [
          {
            required: true,
            message: '部门介绍不能为空',
            trigger: ['blur', 'change']
          },
          {
            min: 1,
            max: 300,
            message: '部门名称只能是1-300位',
            trigger: ['blur']
          }
        ]
      }
    }
  },
  computed: {
    dialogTitle() {
      return this.deptForm.id ? '编辑部门' : '添加子部门'
    }
  },
  created() {
    this.getEmployeeSimple()
  },
  methods: {
    handleClose() {
      // console.log('close')
      this.$emit('update:closeDialog', false)
      this.$refs.deptForm.resetFields()
      this.deptForm = {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      }
    },
    submit() {
      this.$refs.deptForm.validate(async(flag) => {
        if (!flag) return
        if (this.deptForm.id) {
          const res = await reqUpdateDepartsDetails(this.deptForm)
          console.log(res)
          this.$message.success('编辑部门成功')
        } else {
          const res = await reqAddDeparts({
            ...this.deptForm,
            pid: this.nodeData.id
          })
          console.log(res)
          this.$message.success('添加部门成功')
        }
        this.handleClose()
        this.$emit('addDepts')
      })
    },
    async getEmployeeSimple() {
      const { data } = await reqGetEmployeeSimple()
      console.log(data)
      this.peoples = data
    },
    async getDeptsDetails() {
      const { data } = await reqGetDepartsDetails(this.nodeData.id)
      this.deptForm = data
    },
    async upDateDeptsDetails() {

    }
  }
}
</script>
<style scoped></style>
